package com.faradel.pharmapp.dao;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.faradel.pharmapp.entities.Patient;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

@Repository("patientDaoImpl")
public class PatientDaoImpl extends GenericDAOImpl<Patient, Integer> implements PatientDAO
{

    @SuppressWarnings("unchecked")
    public List<Patient> findByColumn(Map<String, String> map)
    {
        Criteria criteria = this.sessionFactory.getCurrentSession().createCriteria(Patient.class);
        for (Map.Entry<String, String> mapEntry : map.entrySet())
        {
            if (mapEntry.getKey().equalsIgnoreCase("patient_id"))
            {
                criteria.add(Restrictions.eq(mapEntry.getKey(), Integer.parseInt(mapEntry.getValue())));
            }
            else if (mapEntry.getKey().equalsIgnoreCase("dob"))
            {
                Date date = null;
                try
                {
                    date = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH).parse(mapEntry.getValue());
                }
                catch (ParseException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                criteria.add(Restrictions.eq(mapEntry.getKey(), date));
            }
            else
            {
                criteria.add(Restrictions.eq(mapEntry.getKey(), mapEntry.getValue()));
            }

        }
        return criteria.list();
    }

    public java.sql.Date sqlDate(java.util.Date calendarDate)
    {
        return new java.sql.Date(calendarDate.getTime());
    }

}
